package com.imooc.springboot.dubbo.nilinili.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.imooc.springboot.dubbo.nilinili.entity.bean.PageBean;
import com.imooc.springboot.dubbo.nilinili.entity.dto.CollectionDTO;
import com.imooc.springboot.dubbo.nilinili.entity.dto.MyCollectionDTO;
import com.imooc.springboot.dubbo.nilinili.res.CodeEnum;
import com.imooc.springboot.dubbo.nilinili.res.ResultFactory;
import com.imooc.springboot.dubbo.nilinili.service.CollectionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/collection")
@Validated
public class CollectionController {
    @Reference
    private CollectionService collectionService;

    /**
     * 查询是否收藏
     *
     * @param photoId
     * @param token
     * @return
     */
    @GetMapping("/isCollected")
    public String isCollected(@RequestParam("photoId") Integer photoId, @RequestHeader("token") String token) {
        CollectionDTO collectionDTO=new CollectionDTO();
        collectionDTO.setPhotoId(photoId);
        boolean b = collectionService.isCollected(collectionDTO, token);
        if (b){
            return ResultFactory.createResult(CodeEnum.SUCCESS,"已收藏");
        }
        else {
            return ResultFactory.createResult(CodeEnum.FAIL,"未收藏");
        }
    }

    /**
     * 变换收藏状态
     * @param collectionDTO
     * @param token
     * @return
     */
    @PostMapping("/toggle")
    public String toggleCollection(@RequestBody CollectionDTO collectionDTO, @RequestHeader("token") String token) {
        boolean b = collectionService.toggleCollection(collectionDTO,token);
        if (b) {
            return ResultFactory.createResult(CodeEnum.SUCCESS,"收藏成功");
        } else {
            return ResultFactory.createResult(CodeEnum.FAIL,"取消收藏成功");
        }
    }

    /**
     * 查询收藏列表
     * @param token
     * @return
     */
    @GetMapping("/list")
    public String listCollection(@RequestHeader("token") String token,@RequestParam Integer pageNum) {

        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询成功",collectionService.listCollection(token, pageNum));
    }

    /**
     * 得到收藏数量
     * @param token
     * @return
     */
    @GetMapping("/count")
    public String countCollection(@RequestHeader("token") String token) {
        return ResultFactory.createResult(CodeEnum.SUCCESS,"查询成功",collectionService.countCollection(token));
    }
}
